package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import android.app.Fragment;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.client.WikiClient;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.Wiki;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.wiki_detail_fragment)
public class WikiDetailFragment extends Fragment {

	@RestService
	WikiClient wikiClient;


	@Bean
	OAuthDto oAuthDto;

	@RestService
	UserClient userClient;

	String page_id;
	
	
	@ViewById(R.id.wiki_sub_title)
	TextView wikiSubTitle;
	
	@ViewById(R.id.back_to_topic)
	Button backToWiki;
	
	@ViewById(R.id.wiki_detail_title)
	TextView wikiMainTitle;
	
	@ViewById(R.id.post_by)
	TextView postBy;
	
	@ViewById(R.id.update_by)
	TextView updateBy;
	
	@ViewById(R.id.wiki_content)
	WebView wikiContent;
	
	String CourseTitle;
	
	@AfterViews
	void initUi()
	{
		if(getArguments().containsKey("pageId"))
		{
			page_id=getArguments().getString("pageId");
			System.out.println("pageId "+page_id);
		}
		
		if(getArguments().containsKey("CourseTitle"))
		{
			CourseTitle=getArguments().getString("CourseTitle");
			wikiMainTitle.setText(CourseTitle);
		}
		gettoken();
	}

	@Trace
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getWiki(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					showMessge(e.getLocalizedMessage());
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@Pref
	SPreferences_ pref;

	@Background
	void getWiki(OAuth requestToken) {

		final MultiValueMap<String, String> mvMap = new LinkedMultiValueMap<String, String>();
		mvMap.add("page_id", page_id);
		mvMap.add("auth_token", "1234");

		// courseDto.setUserId(pref.getId().get());

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doWiki(wikiClient.getWiki(mvMap));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	

	@UiThread
	void doWiki(Wiki wiki) {
		
		
		
		wikiContent.loadData(wiki.getWikiData().getPageContent(), "text/html", "utf-8");
		
		wikiSubTitle.setText(wiki.getWikiData().getPageTitle());
		
		updateBy.setText("Last Updated :"+wiki.getWikiData().getUpdatedUser()+" "+"on "+wiki.getWikiData().getUpdatedDate());
		
		postBy.setText("Created            :"+wiki.getWikiData().getCreatedUser()+" "+"on "+wiki.getWikiData().getCreatedDate().getDate());
		
		System.out.println("WIKI DES"+wiki.getWikiData().getPageTitle());

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}
	
	
	@Click(R.id.back_to_topic)
	void clickBacktoWiki()
	{
		getFragmentManager().popBackStack();
	}

}
